package com.android.commcount.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.interface_.OkHttpCallBack;
import com.android.baselibrary.responce.BaseResponce;
import com.android.baselibrary.tool.CommLoading;
import com.android.baselibrary.tool.CommToast;
import com.android.baselibrary.util.AnimUtil;
import com.android.baselibrary.util.Util;
import com.android.baselibrary.view.Comm_EditView;
import com.android.baselibrary.view.Comm_HeadView;
import com.android.baselibrary.view.GridSpacingItemDecoration;
import com.android.baselibrary.view.MultiStateView;
import com.android.commcount.R;
import com.android.commcount.R2;
import com.android.commcount.adapter.Count_LengthListAdapter;
import com.android.commcount.bean.Count_CompanyInfo;
import com.android.commcount.bean.Count_DetailInfo;
import com.android.commcount.bean.Count_LengthInfo;
import com.android.commcount.manager.API_Manager;
import com.android.commcount.manager.SqlHelper;
import com.android.commcount.ui.view.WordWrapView;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 编辑详情
 */
public class CommCount_EditDetailActivity extends BaseActivity {

    RecyclerView recyclerview_length;
    EditText et_length;
    Comm_EditView et_company;
    WordWrapView wordwrapview;
    TextView tv_count;
    TextView tv_all_length;
    Comm_EditView et_remark;
    public LinearLayout ll_content;
    public View view_bg;
    TextView tv_chuku, tv_ruku;


    Count_DetailInfo countDetailInfo;
    Count_LengthListAdapter adapter_length;

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_adddetails;
    }

    @Override
    protected boolean isAddBgColor() {
        return false;
    }

    @Override
    protected void initView() {
//        getSupportActionBar().hide();// 隐藏ActionBar

//        long editTime = getIntent().getLongExtra("editTime", 0);
//        List<Count_DetailInfo> list = SqlHelper.getHistoryByEditTime(editTime);
//        if (list != null && list.size() > 0) {
//            countDetailInfo = list.get(0);
//        }
        countDetailInfo = (Count_DetailInfo) getIntent().getSerializableExtra("countDetailInfo");

        ll_content = findViewById(com.android.baselibrary.R.id.ll_content);
        view_bg = findViewById(com.android.baselibrary.R.id.view_bg);
        recyclerview_length = findViewById(R.id.recyclerview_length);
        et_length = findViewById(R.id.et_length);
        et_company = findViewById(R.id.et_company);
        wordwrapview = findViewById(R.id.wordwrapview);
        tv_count = findViewById(R.id.tv_count);
        tv_all_length = findViewById(R.id.tv_all_length);
        et_remark = findViewById(R.id.et_remark);
        tv_chuku = findViewById(R.id.tv_chuku);
        tv_ruku = findViewById(R.id.tv_ruku);

        recyclerview_length.setLayoutManager(new GridLayoutManager(mContext, 5));
        recyclerview_length.addItemDecoration(new GridSpacingItemDecoration(5, Util.dip2px(mContext, 10), false));
        adapter_length = new Count_LengthListAdapter(mContext, callBack_length);
        recyclerview_length.setAdapter(adapter_length);

        et_length.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                jisuanLength();
            }
        });

        showWithAnim();
    }

    @Override
    protected void initData() {
        //获取历史长度
        List<Count_LengthInfo> allLength = LitePal.findAll(Count_LengthInfo.class);
        List<String> list_length = new ArrayList<>();
        for (Count_LengthInfo lengthInfo : allLength) {
            list_length.add(lengthInfo.length);
        }
        adapter_length.setData(list_length);

        //获取历史公司名
        List<Count_CompanyInfo> allCompany = LitePal.findAll(Count_CompanyInfo.class);
        for (Count_CompanyInfo companyInfo : allCompany) {
            View view = View.inflate(mContext, R.layout.item_count_company, null);
            TextView tv_text = view.findViewById(R.id.tv_text);
            tv_text.setText(companyInfo.company);
            tv_text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callBack_company.onResult(companyInfo.company);
                }
            });
            wordwrapview.addView(view);
        }

        //初始化数据
        if (countDetailInfo != null) {
            length = countDetailInfo.length;
            inAndOutType = countDetailInfo.inAndOutType;
            company = countDetailInfo.company;
            remark = countDetailInfo.remark;

            tv_count.setText(countDetailInfo.count + "");
            et_length.setText(length + "");
            if (!TextUtils.isEmpty(remark)) {
                et_remark.setText(remark);
            }
            if (!TextUtils.isEmpty(company)) {
                et_company.setText(company);
            }
            checkInOrOut();
        }
    }

    private void checkInOrOut() {
        if (inAndOutType == 0) {
            //出库
            tv_chuku.setBackgroundResource(R.drawable.shape_round4dp_maincolor);
            tv_chuku.setTextColor(Color.parseColor("#ffffff"));

            tv_ruku.setBackgroundResource(R.drawable.shape_round4dp_stroke_00cb56);
            tv_ruku.setTextColor(Color.parseColor("#00cb56"));
        } else {
            tv_chuku.setBackgroundResource(R.drawable.shape_round4dp_stroke_maincolor);
            tv_chuku.setTextColor(getResources().getColor(R.color.mainColor));

            tv_ruku.setBackgroundResource(R.drawable.shape_round4dp_00cb56);
            tv_ruku.setTextColor(Color.parseColor("#ffffff"));
        }
    }

    //计算总长度
    public void jisuanLength() {
        double length = 0.0;
        int count = 0;
        try {
            length = Double.parseDouble(et_length.getText().toString());
            count = Integer.parseInt(tv_count.getText().toString());
        } catch (Exception e) {

        }
        double allLength = com.android.commcount.util.Util.jisuanDouble(length, count);

        tv_all_length.setText(allLength + "");
    }

    CommCallBack callBack_length = new CommCallBack() {
        @Override
        public void onResult(Object obj) {
            String length = (String) obj;
            et_length.setText(length);
            et_length.setSelection(et_length.getText().length());
        }
    };

    CommCallBack callBack_company = new CommCallBack() {
        @Override
        public void onResult(Object obj) {
            String company = (String) obj;
            et_company.setText(company);
        }
    };

    @OnClick({R2.id.csb_ok, R2.id.tv_cancel, R2.id.tv_chuku, R2.id.tv_ruku})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.csb_ok) {
            save();
        } else if (view.getId() == R.id.tv_cancel) {
            dismissWithAnim();
        } else if (view.getId() == R.id.tv_chuku) {
            inAndOutType = 0;
            checkInOrOut();
        } else if (view.getId() == R.id.tv_ruku) {
            inAndOutType = 1;
            checkInOrOut();
        }
    }

    //进入动画
    private void showWithAnim() {
        AnimUtil.fadeIn(view_bg);
        AnimUtil.enterFromBottom(ll_content);
    }

    //退出动画
    public void dismissWithAnim() {
        if (view_bg.getAnimation() != null) {
            view_bg.clearAnimation();
        }
        AnimUtil.fadeOut(view_bg, new CommCallBack() {
            @Override
            public void onResult(Object obj) {
                view_bg.setVisibility(View.GONE);
                ll_content.setVisibility(View.GONE);
                finish();
            }
        });
        AnimUtil.outToBottom(ll_content, null);
    }

    double length = 0;
    int inAndOutType;
    String company;
    String remark;

    public void save() {
        //先保存历史长度
        try {
            length = Double.parseDouble(et_length.getText().toString());
            Count_LengthInfo lengthInfo = new Count_LengthInfo(et_length.getText().toString());
            lengthInfo.save();
        } catch (Exception e) {

        }

        //保存历史公司
        company = et_company.getText();
        if (!TextUtils.isEmpty(company)) {
            Count_CompanyInfo companyInfo = new Count_CompanyInfo(company);
            companyInfo.save();
        }
        //备注信息
        remark = et_remark.getText();

        //保存信息
        countDetailInfo.editTime = System.currentTimeMillis();
        countDetailInfo.hasEditDetail = 1;

        Intent intent = new Intent();
        intent.putExtra("length", length);
        intent.putExtra("inAndOutType", inAndOutType);
        intent.putExtra("company", company);
        intent.putExtra("remark", remark);
        intent.putExtra("hasEditDetail", 1);

        setResult(200, intent);
        finish();
//        dismissWithAnim();
    }

}
